---
title: "Spring Boot Exclue AutoConfiguration"
date: 2020-3-19
categories:
- java
- spring
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgc7d53a0">使用 EnableAutoConfiguration 注解</a></li>
<li><a href="#org8641fa7">TestPropertySource</a></li>
<li><a href="#orgf3ad3a2">配置文件</a></li>
<li><a href="#org411a407">测试类使用单独的Application</a></li>
</ul>
</div>
</div>
<blockquote>
<p>
<a href="https://www.baeldung.com/spring-boot-exclude-auto-configuration-test">https://www.baeldung.com/spring-boot-exclude-auto-configuration-test</a>
</p>
</blockquote>
<div class="outline-2" id="outline-container-orgc7d53a0">
<h2 id="orgc7d53a0">使用 EnableAutoConfiguration 注解</h2>
<div class="outline-text-2" id="text-orgc7d53a0">
<p>
注解中的 exclude 属性排除一些AutoConfiguration类。但不能用来排普通的@Configuration类。
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold; text-decoration: underline;">@RunWith</span>(SpringRunner.<span style="font-weight: bold;">class</span>)
<span style="font-weight: bold; text-decoration: underline;">@SpringBootTest</span>(classes = Application.<span style="font-weight: bold;">class</span>, webEnvironment = <span style="font-weight: bold; text-decoration: underline;">WebEnvironment</span>.DEFINED_PORT)
<span style="font-weight: bold; text-decoration: underline;">@EnableAutoConfiguration</span>(exclude=SecurityAutoConfiguration.<span style="font-weight: bold;">class</span>)
<span style="font-weight: bold;">public</span> <span style="font-weight: bold;">class</span> <span style="font-weight: bold; text-decoration: underline;">ExcludeAutoConfigIntegrationTest</span> {

    <span style="font-weight: bold; text-decoration: underline;">@Test</span>
    <span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">void</span> <span style="font-weight: bold;">givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired</span>() {
        <span style="font-weight: bold; text-decoration: underline;">int</span> <span style="font-weight: bold; font-style: italic;">statusCode</span> = RestAssured.get(<span style="font-style: italic;">"http://localhost:8080/"</span>).statusCode();

        assertEquals(<span style="font-weight: bold; text-decoration: underline;">HttpStatus</span>.OK.value(), statusCode);
    }
}
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org8641fa7">
<h2 id="org8641fa7">TestPropertySource</h2>
<div class="outline-text-2" id="text-org8641fa7">
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold; text-decoration: underline;">@RunWith</span>(SpringRunner.<span style="font-weight: bold;">class</span>)
<span style="font-weight: bold; text-decoration: underline;">@SpringBootTest</span>(classes = Application.<span style="font-weight: bold;">class</span>, webEnvironment = <span style="font-weight: bold; text-decoration: underline;">WebEnvironment</span>.DEFINED_PORT)
<span style="font-weight: bold; text-decoration: underline;">@TestPropertySource</span>(properties = 
 <span style="font-style: italic;">"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration"</span>)
<span style="font-weight: bold;">public</span> <span style="font-weight: bold;">class</span> <span style="font-weight: bold; text-decoration: underline;">ExcludeAutoConfigIntegrationTest</span> {
    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">...</span>
}
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orgf3ad3a2">
<h2 id="orgf3ad3a2">配置文件</h2>
<div class="outline-text-2" id="text-orgf3ad3a2">
<div class="org-src-container">
<pre class="src src-yaml">spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org411a407">
<h2 id="org411a407">测试类使用单独的Application</h2>
<div class="outline-text-2" id="text-org411a407">
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">定义一个 SpringBootApplication, 排除了一个自动配置</span>
<span style="font-weight: bold; text-decoration: underline;">@SpringBootApplication</span>(exclude=SecurityAutoConfiguration.<span style="font-weight: bold;">class</span>)
<span style="font-weight: bold;">public</span> <span style="font-weight: bold;">class</span> <span style="font-weight: bold; text-decoration: underline;">TestApplication</span> {

    <span style="font-weight: bold;">public</span> <span style="font-weight: bold;">static</span> <span style="font-weight: bold; text-decoration: underline;">void</span> <span style="font-weight: bold;">main</span>(<span style="font-weight: bold; text-decoration: underline;">String</span>[] <span style="font-weight: bold; font-style: italic;">args</span>) {
        SpringApplication.run(TestApplication.<span style="font-weight: bold;">class</span>, args);
    }
}
<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">测试类中使用单独的Application</span>
<span style="font-weight: bold; text-decoration: underline;">@RunWith</span>(SpringRunner.<span style="font-weight: bold;">class</span>)
<span style="font-weight: bold; text-decoration: underline;">@SpringBootTest</span>(classes = TestApplication.<span style="font-weight: bold;">class</span>, webEnvironment = <span style="font-weight: bold; text-decoration: underline;">WebEnvironment</span>.DEFINED_PORT)
<span style="font-weight: bold;">public</span> <span style="font-weight: bold;">class</span> <span style="font-weight: bold; text-decoration: underline;">ExcludeAutoConfigIntegrationTest</span> {
    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">...</span>
}
</pre>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2020-3-19</p>
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
